package com.mango.mstock;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.mango.mstock.base.MstockActivity;
import com.mango.mstock.support.MStockGlobalVars;
import com.mango.mstock.support.utils.MStockUtil;

public class MainActivity extends MstockActivity {
	
	private ListView listView;
	private List<Map<String, Object>> listItems;
	private ListViewAdapter listViewAdapter; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		
		listView = (ListView)findViewById(R.id.list_items);    
        listItems = MStockUtil.getMyFavoriteItems();   
        listViewAdapter = new ListViewAdapter(this, listItems);
        listView.setAdapter(listViewAdapter);  
        
        final Button addFavoritebtn = (Button) findViewById(R.id.addFavoritebtn);
        addFavoritebtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
//				AddFavoritePopWindow addPopWindow = new AddFavoritePopWindow();  
//	            addPopWindow.showPopupWindow(addFavoritebtn); 
				showAlertDialog();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		Rect rect = new Rect();
		// /取得整个视图部分,注意，如果你要设置标题样式，这个必须出现在标题样式之后，否则会出错
		getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
//		FloatingUtil.getInstance().statusBarHeight = rect.top;
		MStockGlobalVars.statusBarHeight = 0;

		Log.i("top",""+ rect.top);
	}
	
	@SuppressLint("InflateParams")
	private void showAlertDialog() {  
        LayoutInflater inflater = LayoutInflater.from(this);  
        final View textEntryView = inflater.inflate(  
                R.layout.add_favorite_alert, null);  
        final EditText edtInput = (EditText) textEntryView.findViewById(R.id.edtInput);  
        final AlertDialog.Builder builder = new AlertDialog.Builder(
        		MStockGlobalVars.getCurrentContext());  
        builder.setCancelable(true);  
//        builder.setIcon(R.drawable.logo_welcome);  
        builder.setTitle(R.string.stockcode);  
        builder.setView(textEntryView);  
        builder.setPositiveButton(R.string.confirm,  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) { 
        				String code = edtInput.getText().toString();
        				if (code == null || code.isEmpty()) {
        					Toast.makeText(MStockGlobalVars.getCurrentContext(), 
        		    				"You must input a stock code.", Toast.LENGTH_SHORT).show();
        					return;
        				}
        				MStockUtil.saveMyFavorite(code);
                    }  
                });  
        builder.setNegativeButton(R.string.cancel,  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {  
                    	
                    }  
                });  
        builder.show();  
    }  
	
}